package controllers

import (
	"github.com/gin-gonic/gin"
	"go_user_manager/internal/domain/service"

	"go_user_manager/internal/adapters/controllers/view_model"
	"go_user_manager/pkg/adapters/agin"
	"go_user_manager/pkg/framework"
)

type UserController struct {
	logger      framework.Logger
	userService service.IUserService
}

func NewUserController(logger framework.Logger, userService service.IUserService) *UserController {
	return &UserController{
		logger:      logger,
		userService: userService,
	}
}

func (ctrl *UserController) InitRouter(r *gin.Engine) {
	g := r.Group("/user")
	{
		// 获取用户
		g.GET("", ctrl.GetUser) // @Summary 获取用户
		// 创建用户
		g.POST("", ctrl.CreateUser) // @Summary 创建用户
		// 更新用户
		g.PUT("", ctrl.UpdateUser) // @Summary 更新用户
		// 删除用户
		g.DELETE("", ctrl.DeleteUser) // @Summary 删除用户

		// 登入
		//g.POST("/login", ctrl.Login)
		// 登出
		//g.POST("/logout", ctrl.Logout)
	}
}

// 获取用户信息
func (ctrl *UserController) GetUser(c *gin.Context) {
	id := c.Query("id")
	userName := c.Query("user_name")
	user, err := ctrl.userService.Get(id, userName)
	if err != nil {
		ctrl.logger.Error("GetUser错误: ", err)
		agin.WriteResponse(c, agin.ResponseData{Message: "User not found"}, nil)
		return
	}
	agin.WriteResponse(c, agin.ResponseData{Message: "success", Data: user}, nil)
}

// 更新用户信息
func (ctrl *UserController) UpdateUser(c *gin.Context) {
	var user view_model.UpdateUserReq
	if err := c.ShouldBindJSON(&user); err != nil {
		ctrl.logger.Error("UpdateUser错误: ", err)
		agin.WriteResponse(c, agin.ResponseData{Message: "Invalid input"}, nil)
		return
	}

	userUpdate := view_model.UpdateUserReq{}
	if len(user.ID) > 0 {
		userUpdate.ID = user.ID
	}
	if user.UserName == "" {
		userUpdate.UserName = user.UserName
	}
	if user.Password == "" {
		userUpdate.Password = user.Password
	}
	if user.Email == "" {
		userUpdate.Email = user.Email
	}
	err := ctrl.userService.Update(user)
	if err != nil {
		ctrl.logger.Error("UpdateUser错误: ", err)
		agin.WriteResponse(c, agin.ResponseData{Message: "User not found"}, nil)
		return
	}
	agin.WriteResponse(c, agin.ResponseData{Message: "success"}, nil)
}

// 删除用户
func (ctrl *UserController) DeleteUser(c *gin.Context) {
	id := c.Query("id")
	err := ctrl.userService.Delete(id)
	if err != nil {
		ctrl.logger.Error("DeleteUser错误: ", err)
		agin.WriteResponse(c, agin.ResponseData{Message: "User not found"}, nil)
		return
	}
	agin.WriteResponse(c, agin.ResponseData{Message: "User deleted successfully"}, nil)
}

// 创建用户
func (ctrl *UserController) CreateUser(c *gin.Context) {
	userNew := view_model.CreateUserReq{}
	if err := c.ShouldBindJSON(&userNew); err != nil {
		ctrl.logger.Error("CreateUser错误: ", err)
		agin.WriteResponse(c, agin.ResponseData{Message: "获取参数失败"}, nil)
		return
	}

	err := ctrl.userService.Add(userNew)
	if err != nil {
		ctrl.logger.Error("CreateUser错误: ", err)
		agin.WriteResponse(c, agin.ResponseData{Message: "添加失败"}, nil)
		return
	}

	agin.WriteResponse(c, agin.ResponseData{Message: "success"}, nil)
}
